<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

class WarehouseBill extends DbEloquent{

    protected $table    = 'ygt_warehouse_bill';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'customer_id','title'=>'客户','value'=>0,'is_must'=>1],
        ['name'=>'sn','title'=>'账单编号','value'=>0,'is_must'=>1],
        ['name'=>'pre_money','title'=>'预收款金额','value'=>0,'is_must'=>1],
        ['name'=>'pay_type','title'=>'收款方式','value'=>'','is_must'=>1],
        ['name'=>'send_time','title'=>'发货时间','value'=>'','is_must'=>0],
        ['name'=>'status','title'=>'状态','value'=>'','is_must'=>0],
    ];
    public static function getSn()
    {
        $nowTime            = $_SERVER['REQUEST_TIME'];
        $y4md               = date('Ymd',$nowTime);
        $nowTime0           = strtotime($y4md);
        $y2md               = date('ymd',$nowTime);
        $where              = [
            ['created_at','>',$nowTime0]
        ];
        $count              = self::where($where)->count();
        if($count<10)
        {
            $count          = '0'.$count;
        }
//        $sn                 = 'YSD'.$y2md.$count;
        $sn                 = 'JHD'.$y2md.$count;
        return $sn;
    }
    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where,$data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
    //取客户账单列表,带分页
    public static function getCustomerBillList($companyId,$type,$limit,$offset,$customerId=0)
    {
        $columns            = 'ygt_warehouse_bill.id,ygt_warehouse_bill.customer_id,ygt_warehouse_bill.sn';
        $columns            .= ',ygt_warehouse_bill.money,ygt_warehouse_bill.now_money';
        $columns            .= ',ygt_warehouse_bill.pay_type,ygt_warehouse_bill.status';
        $columns            .= ',ygt_warehouse_bill.created_at';
        $columns            .= ',ygt_customer.customer_name';
        $join               = [
            ['table'=>'ygt_customer','field_l'=>'ygt_customer.id','field_c'=>'=','field_r'=>'ygt_warehouse_bill.customer_id']
        ];
        $where              = [
            'ygt_warehouse_bill.company_id'=>$companyId,
        ];

        if($customerId){
            $where['ygt_warehouse_bill.customer_id'] = $customerId;
        }

        if($type==1)
        {
            $where['ygt_warehouse_bill.status'] = 1;
        }
        else if($type == 0){
            $where['ygt_warehouse_bill.status'] = 0;
        }
        $orderby            = ['ygt_warehouse_bill.id','desc'];
        $groupby            = '';
        $lists              = self::getList($where,$columns,$limit,$offset,$orderby,$groupby,$join)->toArray();
        foreach ($lists as $key=>$val)
        {
            $lists[$key]['created_at_ymd']  = date('Y-m-d',$val['created_at']);
        }
        return $lists;
    }

    //取客户账单列表,带分页
    public static function getCustomerBillListCount($companyId,$type,$customerId=0)
    {
        $columns            = 'ygt_warehouse_bill.id,ygt_warehouse_bill.customer_id,ygt_warehouse_bill.sn';
        $columns            .= ',ygt_warehouse_bill.money,ygt_warehouse_bill.now_money';
        $columns            .= ',ygt_warehouse_bill.pay_type,ygt_warehouse_bill.status';
        $columns            .= ',ygt_warehouse_bill.created_at';
        $columns            .= ',ygt_customer.customer_name';
        $join               = [
            ['table'=>'ygt_customer','field_l'=>'ygt_customer.id','field_c'=>'=','field_r'=>'ygt_warehouse_bill.customer_id']
        ];
        $where              = [
            'ygt_warehouse_bill.company_id'=>$companyId,
        ];

        if($customerId){
            $where['ygt_warehouse_bill.customer_id'] = $customerId;
        }

        if($type==1)
        {
            $where['ygt_warehouse_bill.status'] = 1;
        }
        else if($type == 0){
            $where['ygt_warehouse_bill.status'] = 0;
        }
        $orderby            = ['ygt_warehouse_bill.id','desc'];
        $groupby            = '';
        $count              = self::getList($where,$columns,'','',$orderby,$groupby,$join)->count();
        return $count;
    }


}
